﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;

namespace Business {
    /// <summary>
    /// Class for password and url decryption
    /// </summary>
    public class Decryption {
        /// <summary>
        /// Decrypts the string from encrypted password stored in the database.
        /// </summary>
        /// <param name="txtenc">The txtenc.</param>
        /// <returns>returns the decrypted string</returns>
        public string DecryptString(string txtenc) {

            txtenc = txtenc.Replace(" ", "+");
            byte[] keyArray;

            //get the byte code of the string
            byte[] toEncryptArray = Convert.FromBase64String(txtenc);

            Key myKey = new Key();
            string key = myKey.ToString();

            //if hashing was used get the hash code with regards to your key
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            //release any resource held by the MD5CryptoServiceProvider
            hashmd5.Clear();


            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            //set the secret key for the tripleDES algorithm
            tdes.Key = keyArray;
            //mode of operation. there are other 4 modes. We choose ECB(Electronic code Book)
            tdes.Mode = CipherMode.ECB;
            //padding mode(if any extra byte added)
            tdes.Padding = PaddingMode.PKCS7;


            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            //Release resources held by TripleDes Encryptor                
            tdes.Clear();
            //return the Clear decrypted TEXT
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
        /// <summary>
        /// Decodes the URL.
        /// </summary>
        /// <param name="encoded">The encoded.</param>
        /// <returns>returns the decoded string</returns>
        public string DecodeUrl(string encoded) {
            Key myKey = new Key();
            string key = myKey.ToString();

            MACTripleDES des = new MACTripleDES();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            des.Key = md5.ComputeHash(Encoding.UTF8.GetBytes(key));

            string decoded = HttpUtility.UrlDecode(encoded);
            // in the act of url encoding and decoding, plus (valid base64 value) gets replaced with space (invalid base64 value). this reverses that.
            decoded = decoded.Replace(" ", "+");
            string value = Encoding.UTF8.GetString(Convert.FromBase64String(decoded.Split('-')[1]));
            string savedHash = Encoding.UTF8.GetString(Convert.FromBase64String(decoded.Split('-')[0]));
            string calculatedHash = Encoding.UTF8.GetString(des.ComputeHash(Encoding.UTF8.GetBytes(value)));

            if (savedHash != calculatedHash) return null;

            return value;
        }
    }
}
